package com.littlebee.storysuper.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.littlebee.storysuper.R;
import com.littlebee.storysuper.entry.User;
import com.littlebee.storysuper.utils.LogUtils;
import com.littlebee.storysuper.utils.NetWorkUtils;
import com.littlebee.storysuper.utils.Utils;
import com.littlebee.storysuper.widget.CostomCircleCountDown;

import org.json.JSONObject;

import java.util.HashMap;

/**
 * 启动页
 */
public class LoadingActivity extends AppCompatActivity {
    RelativeLayout show_bg;
    CostomCircleCountDown countdownProgressView;
    boolean isFinsh=false;
    Button btn_submit;
    ImageView iv;
    String url="https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=3337529856,442452951&fm=26&gp=0.jpg";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loading);
        show_bg=(RelativeLayout)findViewById(R.id.show_bg);
        btn_submit=(Button)findViewById(R.id.btn_submit);
        iv=(ImageView)findViewById(R.id.iv);
        //模拟显示图片
//        Glide.with(LoadingActivity.this)
//        .load(url)
//                .diskCacheStrategy(DiskCacheStrategy.ALL)
//                .into(iv);

        btn_submit.setVisibility(View.GONE);
        countdownProgressView = (CostomCircleCountDown) findViewById(R.id.costomCirc);
        countdownProgressView.setmMaxTime(5);
        countdownProgressView.startAnim();
        countdownProgressView.setOnFinishAnimListener(new CostomCircleCountDown.OnFinishAnimListener() {
            @Override
            public void onFinish() {
                Intent intent=new Intent(LoadingActivity.this,MainActivity.class);
                startActivity(intent);
                finish();
            }
        });
        getInfo();
        countdownProgressView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                countdownProgressView.stopAnim();
                Intent intent=new Intent(LoadingActivity.this,MainActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }

    /**
     * 获取启动图
     */
    public void getInfo() {
        User user= Utils.getUserEntry();
        if(user!=null) {
            HashMap<String, String> map = new HashMap<>();
            map.put("phoneId", user.getMobile());
//        map.put("password","123456");
            NetWorkUtils.post(LoadingActivity.this, "getenglishdesinfo", map, false,new NetWorkUtils.IListener() {

                @Override
                public void onSuccess(String result, JSONObject resObj) {
                    LogUtils.i("XHX", "XHXSUCC:" + result);
                    // TODO: 2017/8/16   下载图片
//                    Glide.with(LoadingActivity.this)
//                            .load(url)
//                            .downloadOnly(500, 500);
                }

                @Override
                public void onError(String result, String code, String msg) {
                    LogUtils.i("XHX", "XHXERROR:" + result);

                }
            });
        }
    }
    @Override
    public void onBackPressed() {
        countdownProgressView.stopAnim();
        super.onBackPressed();
    }
}
